/*
 * @Title: 微信支付 - 单独拆分适配多域名支付
 * @Author: JSL
 * @Date:   2018-11-19
 * @Last Modified by:   JSL
 * @Last Modified time: 2019-01-08
 * @remark:微信支付来源
 * 1.填写订单 成功 - 支付成功页+orderId  失败 - 待支付页+orderId
 * 2.订单详情待支付
 *
 * 1.拼团 - 支付成功跳转拼团支付成功页面
 * 2.拼团 - 订单详情页面支付成功/失败都是在原页面
 *
 * 拼接跳转地址  /html/WxPay.html?mallUrl='+XXX+'&ajaxUrl='+XXX+'&orderId='+XXX+'&openid='+XXX+''&groupId='+XXX+''&activityType='+XXX+''&payType='+XXX
 * 只考虑填写订单的话 就有 拼团成功页-拼团订单页、订单成功页-订单详情 四个跳转
 *
 * 181128 - 需要更改四个页面  181226 - 新增票选页面
 * 1.-orderFillling
 * 2.-myOrder
 * 3.orderDetail
 * 4.PtOrderDetails
 * 5.FPtVoteDetails
 */

var dataInfo = new Object(),
  template = {
    init: function() {
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          voteItemCode: getQueryString('voteItemCode'), //票选投票详情ID
          relationId: getQueryString('relationId'), //票选关系id
          mallUrl: getQueryString('mallUrl'), //原有商城域名地址
          addressPayUrl: getQueryString('ajaxUrl'), //支付域名
          orderId: getQueryString('orderId'), //订单id
          openid: getQueryString('openid'), //openid
          groupId: getQueryString('groupId'),
          activityType: getQueryString('activityType'),
          virtualgoodsID: getQueryString('virtualgoodsID'),
          activityId: getQueryString('activityId'),
          inviterId: getQueryString('inviterId'),//域名不同 userId不同 无法赋值
          isWx: is_weixn(),
          wxInfo: {}, //微信支付信息
          payType: getQueryString('payType'), //1 - 普通支付（分销商品、大礼包）、 2 - 普通订单支付 、3 - 拼团支付 、 4 - 拼团订单支付 、 5 - 票选支付
        },
        methods: {
          getUrl: function() {
            dataInfo.jumpUrl = this.mallUrl + '/html/';
            dataInfo.failUrl = this.mallUrl + '/html/';
            if (this.payType == 2) { //订单详情 - 我的订单的付款 失败成功都走订单详情
              dataInfo.jumpUrl += 'orderDetails.html?orderId=' + this.orderId + '&tag=1';
              dataInfo.failUrl += 'orderDetails.html?orderId=' + this.orderId;
            } else if (this.payType == 3) { //拼团商品付款 - 朱浩捷页面
              dataInfo.jumpUrl += 'PtPaySuccess.html?orderId=' + this.orderId + '&groupId=' + this.groupId + '&activityType=' + this.activityType + '&virtualgoodsID=' + this.virtualgoodsID + '&activityId=' + this.activityId;
              dataInfo.failUrl += 'PtOrderDetails.html?orderId=' + this.orderId;
            } else if (this.payType == 4) { //拼团订单付款
              dataInfo.jumpUrl += 'PtOrderDetails.html?orderId=' + this.orderId;
              dataInfo.failUrl += 'PtOrderDetails.html?orderId=' + this.orderId;
            } else if (this.payType == 5) { //票选投票
              dataInfo.jumpUrl += 'FPtPayCall.html?orderId=' + this.orderId + '&voteItemCode=' + this.voteItemCode + '&inviterId=' + this.inviterId;
              dataInfo.failUrl += 'FPtVoteDetails.html?voteItemCode=' + this.voteItemCode + '&inviterId=' + this.inviterId;
            } else { //正常的订单付款
              dataInfo.jumpUrl += 'orderFinish.html?orderId=' + this.orderId;
              dataInfo.failUrl += 'orderDetails.html?orderId=' + this.orderId;
            };
            this.payType == 5 ? this.toPxPay() : this.toPay();
          },
          toPay: function() {
            dataInfo.payInfo = new Object();
            dataInfo.payInfo.orderId = this.orderId;
            dataInfo.payInfo.ptType = 'fx';
            dataInfo.payInfo.channel = 'h5fxcwx';
            dataInfo.payInfo.openid = this.openid;
            ajaxTemplate(this.addressPayUrl + SERVICE.payUrl, function(data) {
              console.log(data);
              if (data.tag == 0) { //wxPay
                dataInfo.init.wxInfo = data.data.result;
                if (typeof WeixinJSBridge == "undefined") {
                  if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result), false);
                  } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result));
                    document.attachEvent('onWeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result));
                  };
                } else {
                  console.log(data.data.result);
                  dataInfo.init.onBridgeReady(data.data.result);
                };
              } else {
                dataInfo.init.$toast.clear();
                dataInfo.init.$toast(data.errMsg);
              };
            }, dataInfo.payInfo);
          },
          toPxPay: function() {
            dataInfo.payInfo = new Object();
            dataInfo.payInfo.relationId = this.orderId;
            dataInfo.payInfo.payType = 'WxH5';
            dataInfo.payInfo.userid = getQueryString('userid');
            dataInfo.payInfo.openId = this.openid;
            ajaxTemplate(this.addressPayUrl + SERVICE.pxSign, function(data) {
              console.log(data);
              if (data.tag == 0) { //wxPay
                dataInfo.init.wxInfo = data.data.result;
                if (typeof WeixinJSBridge == "undefined") {
                  if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result), false);
                  } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result));
                    document.attachEvent('onWeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result));
                  };
                } else {
                  console.log(data.data.result);
                  dataInfo.init.onBridgeReady(data.data.result);
                };
              } else {
                dataInfo.init.$toast.clear();
                dataInfo.init.$toast(data.errMsg);
              };
            }, dataInfo.payInfo);
          },
          onBridgeReady: function(msg) { //wxPay
            dataInfo.init.$toast.clear();
            $(document).on('WeixinJSBridgeReady', function() {
              WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                  "appId": msg.appId, //公众号名称，由商户传入
                  "timeStamp": msg.timeStamp, //时间戳，自1970年以来的秒数
                  "nonceStr": msg.nonceStr, //随机串
                  "package": msg.package_str,
                  "signType": "MD5", //微信签名方式：
                  "paySign": msg.paySign //微信签名
                },
                function(res) {
                  console.log(res);
                  if (res.err_msg == "get_brand_wcpay_request:ok") { //支付成功三种跳转 普通支付成功页、订单支付成功页、拼团支付成功页
                    location.href = dataInfo.jumpUrl; //订单完成
                  } else { //支付失败 三种状态 普通支付失败跳转订单详情、订单支付失败仍停在订单支付页、拼团支付失败跳转拼团订单支付失败页
                    location.href = dataInfo.failUrl; //订单详情
                  };
                });
            });

          },
        },
      });
      // 初始化数据
      dataInfo.init.getUrl();
    },
  };

template.init();
